/*
 * This file is part of "IdeWiki" project.
 *
 * Copyright (c) 2008 Hendrik Ebel
 * All Rights Reserved.
 *
 * Please visit http://code.google.com/p/idewiki for updates and contact.
 *
 * --LICENSE NOTICE--
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; If not, see <http://www.gnu.org/licenses/>.
 * --LICENSE NOTICE--
 */
package ws.ebel.idewiki.browser;

import java.util.List;

import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jdt.core.IJavaModel;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.browser.LocationAdapter;
import org.eclipse.swt.browser.LocationEvent;
import org.eclipse.swt.browser.ProgressEvent;
import org.eclipse.swt.browser.ProgressListener;
import org.eclipse.swt.browser.StatusTextEvent;
import org.eclipse.swt.browser.StatusTextListener;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Cursor;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.program.Program;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.IViewSite;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.ide.IDE;

import ws.ebel.idewiki.Activator;
import ws.ebel.idewiki.IdeWikiConstants;
import ws.ebel.idewiki.editors.IdeWikiEditor;
import ws.ebel.idewiki.util.CommonHelper;
import ws.ebel.idewiki.xmlrpc.RpcClient;
import ws.ebel.idewiki.xmlrpc.SnipBean;

/**
 * The IDE Wiki Browser show the wiki web content.
 * 
 * @author Hendrik Ebel
 */
public final class IdeWikiBrowser implements IPropertyChangeListener {

    /**
     * A helper that can ignore events. It is needed because the browser fires
     * events for all the resources a URL requires, not just the URL itself.
     * This is problematic because I need to manage the history manually, and
     * don't want lots of stuff appearing in the history that shouldn't. The
     * history is populated in part by location events because the events are
     * the only way to know that a link has been clicked in the browser.
     * <P>
     * The pattern to set LocationListener.listen to false prior to loading
     * external URLs and then setting back to true with the browser has finished
     * loading the page. This is determined by the progress monitor...nasty!
     */
    private class LocationListener extends LocationAdapter {
        
        /** The listen. */
        boolean listen = true;

        /**
         * Changing.
         * 
         * @param event the event
         */
        public void changing(LocationEvent event) {
            if (event != null) {
                IdeWikiBrowser.this.followLink(event);
                // ignore edit and ide links
                if (!(isEditLocation(event.location) || isIdeLinkLocation(event.location))) {
                    String location = event.location;

                    if (isQuickLoginLocation(event.location)) {
                        location = Activator.getDefault().getWikiUrlPreference() + IdeWikiConstants.START_URL_IDENTIFIER;
                    }
                    textUrl.setText(location);
                    history.add(location);
                }
            }
        }

        /**
         * Checks if is quick login location.
         * 
         * @param location the location
         * 
         * @return true, if is quick login location
         */
        private boolean isQuickLoginLocation(String location) {
            if (location.equals(CommonHelper.getStartUrl(true))) {
                return true;
            } else {
                return false;
            }
        }

        /**
         * Changed.
         * 
         * @param event the event
         */
        public void changed(LocationEvent event) {
        }
    }

    /** The waiter. */
    private Cursor waiter;

    /** The browser. */
    private Browser browser;

    /** The history. */
    private BrowserHistory history;

    /** The location listener. */
    private LocationListener locationListener;

    /** The progress bar. */
    private ProgressBar progressBar;

    /** The status. */
    private Label status;

    /** The display area. */
    private Composite displayArea;

    /** The home item. */
    private ToolItem homeItem;

    /** The back item. */
    private ToolItem backItem;

    /** The forward item. */
    private ToolItem forwardItem;

    /** The refresh item. */
    private ToolItem refreshItem;

    /** The stop item. */
    private ToolItem stopItem;

    /** The launch item. */
    private ToolItem launchItem;

    /** The text url. */
    private Text textUrl;

    /** The toolbar comp. */
    private Composite toolbarComp;

    /** The refresh image. */
    private Image refreshImage;

    /** The stop image. */
    private Image stopImage;

    /** The launch image. */
    private Image launchImage;

    /** The tab folder. */
    private CTabFolder tabFolder;

    /** The start url. */
    private String startUrl;

    /** The site. */
    private IViewSite site;

    /** The index. */
    private int index;

    /** The busy. */
    boolean busy;
    
    /** auto login flag */
    boolean autoLoginEnabled = Activator.getDefault().getIsAutoLoginEnabled();
    

    /**
     * Constructor.
     * 
     * @param site the site
     * @param wikiEditor      * @param tabFolder the tab folder
     */
    public IdeWikiBrowser(CTabFolder tabFolder, IViewSite site) {
        this.tabFolder = tabFolder;
        this.site = site;
        startUrl = CommonHelper.getStartUrl();
        history = new BrowserHistory();
        if (!autoLoginEnabled) {
            history.add(startUrl);
        }

        RpcClient.getInstance(); // init rpc client
        Activator.getDefault().getPreferenceStore().addPropertyChangeListener(this);
        createPartControl(tabFolder);
    }

    /**
     * Dispose.
     */
    public void dispose() {
        waiter.dispose();
        Activator.getDefault().getPreferenceStore().removePropertyChangeListener(this);
        browser.dispose();
        stopImage.dispose();
        refreshImage.dispose();
        launchImage.dispose();
        tabFolder.dispose();
        RpcClient.getInstance().dispose();
    }

    /**
     * Creates the part control.
     * 
     * @param parent the parent
     */
    private void createPartControl(Composite parent) {
        displayArea = new Composite(parent, SWT.NONE);
        setDisplayAreaLayout(new GridLayout());
        toolbarComp = new Composite(displayArea, SWT.NONE);
        browser = new Browser(displayArea, SWT.BORDER);
        createToolBar();
        createUrlTextField(toolbarComp);
        createBrowserFrame();
        createStatusBar();
        addProgressListener();
        waiter = new Cursor(Display.getDefault(), SWT.CURSOR_WAIT);
    }

    /**
     * Creates the status bar.
     */
    private void createStatusBar() {
        Composite statusBarComp = new Composite(displayArea, SWT.NONE);
        GridLayout statusLayout = new GridLayout();
        statusLayout.numColumns = 2;
        statusLayout.marginWidth = 0;
        statusLayout.marginHeight = 0;
        statusBarComp.setLayout(statusLayout);
        statusBarComp.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_CENTER | GridData.FILL_HORIZONTAL));

        status = new Label(statusBarComp, SWT.NONE);
        progressBar = new ProgressBar(statusBarComp, SWT.NONE);

        GridData data = new GridData();
        data.horizontalAlignment = GridData.FILL;
        data.grabExcessHorizontalSpace = true;
        data.horizontalIndent = 2;
        status.setLayoutData(data);

        data = new GridData(GridData.HORIZONTAL_ALIGN_END);
        data.widthHint = 80;
        data.grabExcessHorizontalSpace = false;
        progressBar.setLayoutData(data);

        browser.addStatusTextListener(new StatusTextListener() {
            public void changed(StatusTextEvent event) {
                status.setText(event.text);
            }
        });
    }

    /**
     * Sets the focus.
     */
    public void setFocus() {
    }

    /**
     * Creates the browser frame.
     */
    private void createBrowserFrame() {
        GridData data = new GridData(GridData.FILL_BOTH);
        data.grabExcessHorizontalSpace = true;
        data.grabExcessVerticalSpace = true;
        browser.setLayoutData(data);
        locationListener = new LocationListener();
        browser.addLocationListener(locationListener);
        browser.setUrl(startUrl);
    }

    /**
     * Sets the display area layout.
     * 
     * @param gridLayout the grid layout
     */
    private void setDisplayAreaLayout(GridLayout gridLayout) {
        gridLayout.numColumns = 1;
        gridLayout.marginHeight = 0;
        gridLayout.marginWidth = 0;
        gridLayout.verticalSpacing = 2;
        displayArea.setLayout(gridLayout);
        displayArea.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
    }

    /**
     * Adds the progress listener.
     */
    private void addProgressListener() {
        browser.addProgressListener(new ProgressListener() {
            public void changed(ProgressEvent event) {
                if (event.total == 0){
                    return;
                }
                
                stopItem.setEnabled(true);
                int ratio = event.current * 100 / event.total;
                if (progressBar != null){
                    progressBar.setSelection(ratio);
                }
                busy = event.current != event.total;
                if (!busy) {
                    index = 0;
                }
            }

            public void completed(ProgressEvent event) {
                if (progressBar != null) {
                    progressBar.setSelection(0);
                }
                busy = false;
                index = 0;
                backItem.setEnabled(browser.isBackEnabled());
                forwardItem.setEnabled(browser.isForwardEnabled());
                stopItem.setEnabled(false);
            }
        });
    }

    /**
     * Creates the tool bar.
     */
    private void createToolBar() {
        GridLayout topLayout = new GridLayout();
        topLayout.numColumns = 2;
        topLayout.marginWidth = 0;
        topLayout.marginHeight = 0;
        toolbarComp.setLayout(topLayout);
        toolbarComp.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_CENTER | GridData.FILL_HORIZONTAL));

        refreshImage = CommonHelper.createImage("icons/refresh.gif");
        stopImage = CommonHelper.createImage("icons/stop.gif");
        launchImage = CommonHelper.createImage("icons/launch.gif");

        Listener listener = new Listener() {
            public void handleEvent(Event event) {
                ToolItem item = (ToolItem) event.widget;
                if (item == backItem) {
                    browser.back();
                } else if (item == forwardItem) {
                    browser.forward();
                } else if (item == stopItem) {
                    browser.stop();
                } else if (item == refreshItem) {
                    browser.refresh();
                }
            }
        };

        ToolBar bar = new ToolBar(toolbarComp, SWT.RIGHT | SWT.FLAT);

        homeItem = new ToolItem(bar, SWT.PUSH);
        homeItem.setText(Activator.getResourceString("WikiBrowser.wikiHome"));
        homeItem.setToolTipText(Activator.getResourceString("WikiBrowser.wikiHomeTooltip"));
        homeItem.setImage(PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_TOOL_UNDO));
        homeItem.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                IdeWikiBrowser.this.browser.setUrl(startUrl);
            }
        });

        ToolItem separatorOneItem = new ToolItem(bar, SWT.SEPARATOR);

        backItem = new ToolItem(bar, SWT.PUSH);
        backItem.setToolTipText(Activator.getResourceString("WikiBrowser.backTooltip"));
        backItem.setImage(PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_TOOL_BACK));
        backItem.setEnabled(browser.isBackEnabled());
        backItem.addListener(SWT.Selection, listener);

        forwardItem = new ToolItem(bar, SWT.PUSH);
        forwardItem.setToolTipText(Activator.getResourceString("WikiBrowser.forwardTooltip"));
        forwardItem.setImage(PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_TOOL_FORWARD));
        forwardItem.setEnabled(browser.isForwardEnabled());
        forwardItem.addListener(SWT.Selection, listener);

        refreshItem = new ToolItem(bar, SWT.PUSH);
        refreshItem.setToolTipText(Activator.getResourceString("WikiBrowser.refreshTooltip"));
        refreshItem.setEnabled(true);
        refreshItem.setImage(refreshImage);
        refreshItem.addListener(SWT.Selection, listener);

        stopItem = new ToolItem(bar, SWT.PUSH);
        stopItem.setToolTipText(Activator.getResourceString("WikiBrowser.stopTooltip"));
        stopItem.setEnabled(false);
        stopItem.setImage(stopImage);
        stopItem.addListener(SWT.Selection, listener);

        ToolItem separatorTwoItem = new ToolItem(bar, SWT.SEPARATOR);

        launchItem = new ToolItem(bar, SWT.PUSH);
        launchItem.setText(Activator.getResourceString("WikiBrowser.launch"));
        launchItem.setToolTipText(Activator.getResourceString("WikiBrowser.launchTooltip"));
        launchItem.setEnabled(true);
        launchItem.setImage(launchImage);
        launchItem.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                Program.launch(browser.getUrl());
            }
        });
    }

    /**
     * Creates the url text field.
     * 
     * @param parent the parent
     */
    private void createUrlTextField(Composite parent) {
        textUrl = new Text(parent, SWT.SINGLE | SWT.FLAT | SWT.BORDER);

        GridData data = new GridData();
        data.horizontalAlignment = GridData.FILL;
        data.grabExcessHorizontalSpace = true;
        textUrl.setLayoutData(data);

        textUrl.addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent e) {
                if (e.character == SWT.CR) {
                    IdeWikiBrowser.this.browser.setUrl(textUrl.getText());
                }
            }
        });
    }

    /**
     * Follow link.
     * 
     * @param event the event
     */
    private void followLink(LocationEvent event) {
        locationListener.listen = false;
        backItem.setEnabled(true);
        String location = event.location;

        if (location != null && isEditLocation(location)) {
            openEditOrNewLocation(location, ViewType.EditView);
            event.doit = false;
        } else if (location != null && isIdeLinkLocation(location)) {
            browser.setEnabled(false);
            openEclipseLocation(location);
            event.doit = false;
            browser.setEnabled(true);
        } else if (location != null && isNewLocation(location)){
            openEditOrNewLocation(location, ViewType.NewView);
            event.doit = false;
        }
    }

    /**
     * Open eclipse location.
     * 
     * @param location the location
     */
    private void openEclipseLocation(String location) {
        String projectName = CommonHelper.getProjectName(location);
        String resourceName = CommonHelper.getResourceName(location);
        IJavaProject project = getJavaProject(projectName);

        try {
            if (project != null && project.isConsistent()) {

                try {
                    IType type = project.findType(resourceName);

                    if (type != null) {
                        IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
                        IWorkbenchWindow workbenchWindow = site.getWorkbenchWindow();
                        IWorkbenchPage page = workbenchWindow.getActivePage();

                        IDE.openEditor(page, root.getFile(type.getPath()));
                        browser.refresh();//.setUrl((String) history.getCurrent());
                    } else {
                        Activator.getDefault().reportError("Error",
                                "No type with name '" + resourceName + "' in project: '" + projectName + "' available!");
                        browser.refresh();//.setUrl((String) history.getCurrent());
                    }

                } catch (PartInitException e) {
                    Activator.getDefault().logAndReport("Error",
                            "Can't open type with name '" + resourceName + "' in project: '" + projectName + "'!", e);
                    browser.refresh();//.setUrl((String) history.getCurrent());
                } catch (JavaModelException e1) {
                    Activator.getDefault().logAndReport("Error",
                            "Can't find type with name '" + resourceName + "' in project: '" + projectName + "'!", e1);
                    browser.refresh();//.setUrl((String) history.getCurrent());
                }

            } else {
                Activator.getDefault().reportError("Error",
                        "Can't find project with name '" + projectName + "' or it is not consistent!");
                browser.refresh();//setUrl((String) history.getCurrent());
            }
        } catch (JavaModelException e) {
            Activator.getDefault().logAndReport("JavaModelException", "Can't open type with name '" + resourceName + "'!", e);
            browser.refresh();//.setUrl((String) history.getCurrent());
        }

        locationListener.listen = true;
    }

    /**
     * Gets the java project.
     * 
     * @param projectName the project name
     * 
     * @return the project or null
     */
    private IJavaProject getJavaProject(String projectName) {
        if (projectName == null) {
            return null;
        }

        IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
        IJavaModel model = JavaCore.create(root);

        return model.getJavaProject(projectName);
    }
    
    /**
     * Open an edit location with edit view.
     * 
     * @param location the location
     * @param viewType the view type
     */
    private void openEditOrNewLocation(String location, ViewType viewType) {
        String snipName = getCurrentSnipName(location);

        if (tabFolder != null) {
            if (snipName != null) {
                SnipBean snip = null;
                if(viewType == ViewType.NewView){
                    snip = new SnipBean(snipName , getParentSnipName(location));
                } else {
                    snip = RpcClient.getInstance().getSnipContent(snipName);
                }
                

                if (snip != null && snip.getContent() != null) {
                    List<IdeWikiEditor> wikiEditorList = Activator.getDefault().getWikiEditorList();
                    IdeWikiEditor reuseWikiEditor = null;
                    for (IdeWikiEditor curEditor : wikiEditorList) {
                        if (curEditor.getLocation().equals(location)) {
                            if(!curEditor.getTabItem().isDisposed()){
                                reuseWikiEditor = curEditor;
                            }
                        }
                    }

                    CTabItem newEditItem = null;
                    if (reuseWikiEditor == null) {
                        newEditItem = new CTabItem(tabFolder, SWT.CLOSE);
                        if(viewType == ViewType.NewView){
                            newEditItem.setText("New Snip");
                            newEditItem.setImage(CommonHelper.createImage("icons/editor.gif"));
                        } else {
                            newEditItem.setText(getStyledSnipName(snip.getDecodeSnipName()));
                            newEditItem.setToolTipText("Snip: " + snip.getDecodeSnipName());
                            newEditItem.setImage(CommonHelper.createImage("icons/editor.gif"));
                        }

                        IdeWikiEditor wikiEditor = new IdeWikiEditor(tabFolder, newEditItem, location, snip, viewType);

                        newEditItem.setControl(wikiEditor.getEditorArea());
                        newEditItem.setData(wikiEditor);
                        Activator.getDefault().addWikiEditor(wikiEditor);
                    } else {
                        newEditItem = reuseWikiEditor.getTabItem();
                        if(viewType == ViewType.EditView){
                            reuseWikiEditor.updateSnipContent(snip);
                            reuseWikiEditor.getTabItem().isDisposed();
                        }
                    }

                    tabFolder.setSelection(newEditItem);
                    browser.refresh();

                } else {
                    Activator.getDefault().reportError("SnipSnap Error", "Error by snip loading! Snip Name = " + snipName);
                    browser.refresh();//setUrl((String) history.getCurrent());
                }
            } else {
                Activator.getDefault().reportError("SnipSnap Error", "Can't retrieve snip name for loading!");
                browser.refresh();//setUrl((String) history.getCurrent());
            }

        } else {
            Activator.getDefault().reportError("TabFolder Error", "TabFolder is null!");
            browser.refresh();//setUrl((String) history.getCurrent());
        }

        locationListener.listen = true;
    }

    /**
     * Gets the parent snip name.
     * 
     * @param aSnipName      * @param location the location
     * 
     * @return the parent snip name
     */
    private String getParentSnipName(String location) {
        String result = null;
        String wikiUrl = Activator.getDefault().getWikiUrlPreference();

        if (location.startsWith(wikiUrl + IdeWikiConstants.NEW_LINK_PREFIX + IdeWikiConstants.PARENT_NAME_PREFIX)) {
            String purNewUrl = wikiUrl + IdeWikiConstants.NEW_LINK_PREFIX + IdeWikiConstants.PARENT_NAME_PREFIX;
            result = location.substring(purNewUrl.length());
            // remove space placeholder
            if (result.contains("+")) {
                result = result.replace("+", " ");
            }
        }

        return result;
    }

    /**
     * Gets the styled snip name.
     * 
     * @param snipName the snip name
     * 
     * @return the styled snip name
     */
    private String getStyledSnipName(String snipName) {
        String result = "";

        if (snipName.contains("/") && snipName.length() > 23) {
            result = snipName.substring(snipName.lastIndexOf("/") + 1, snipName.length());
        } else {
            result = snipName;
        }

        return result;
    }

    /**
     * Get the snip name from like url
     * http://127.0.0.1:8668/exec/edit?name=myBooks/novel
     * 
     * @param location the location
     * 
     * @return the snip name like myBooks/novel
     */
    private String getCurrentSnipName(String location) {
        String result = null;
        String wikiUrl = Activator.getDefault().getWikiUrlPreference();

        if (location.startsWith(wikiUrl + IdeWikiConstants.EDIT_LINK_PREFIX)) {
            String purEditUrl = wikiUrl + IdeWikiConstants.EDIT_LINK_PREFIX;
            result = location.substring(purEditUrl.length());
            // remove space placeholder
            if (result.contains("+")) {
                result = result.replace("+", " ");
            }
        } else if (location.startsWith(wikiUrl + IdeWikiConstants.NEW_LINK_PREFIX + IdeWikiConstants.NEW_NAME_PREFIX)){
            String purNewUrl = wikiUrl + IdeWikiConstants.NEW_LINK_PREFIX + IdeWikiConstants.NEW_NAME_PREFIX;
            result = location.substring(purNewUrl.length());
            // remove space placeholder
            if (result.contains("+")) {
                result = result.replace("+", " ");
            }
        } else if(location.startsWith(wikiUrl + IdeWikiConstants.NEW_LINK_PREFIX + IdeWikiConstants.PARENT_NAME_PREFIX)){
            result = ""; 
        }

        return result;
    }

    /**
     * Checks if is edit location.
     * 
     * @param location the location
     * 
     * @return true, if is edit location
     */
    private boolean isEditLocation(String location) {
       return location.startsWith(Activator.getDefault().getWikiUrlPreference() + IdeWikiConstants.EDIT_LINK_PREFIX);
    }
    
    /**
     * Checks if is new location.
     * 
     * @param location the location
     * 
     * @return true, if is new location
     */
    private boolean isNewLocation(String location) {
        return location.startsWith(Activator.getDefault().getWikiUrlPreference() + IdeWikiConstants.NEW_LINK_PREFIX);
     }

    /**
     * Checks if is ide link location.
     * 
     * @param location like http://127.0.0.1:8668/space/IdeMacro?p=MyProject&c=org.snipsnap.net.SnipLockServlet
     * 
     * @return true if location contains "/space/IdeMacro?p="
     */
    private boolean isIdeLinkLocation(String location) {
        return location.contains(IdeWikiConstants.IDE_MACRO_LOCATION_IDENTIFIER) || (location.contains("&c=") && location.contains("&p="));
    }

    /**
     * Refresh.
     */
    public void refresh() {
        browser.refresh();
    }

    /**
     * Open location.
     * 
     * @param location the location
     */
    public void openLocation(String location) {
        if (isEditLocation(location)) {
            openEditOrNewLocation(location, ViewType.EditView);
        } else if(isNewLocation(location)){
            openEditOrNewLocation(location, ViewType.NewView);
        }else {
            browser.setUrl(location);
        }
    }

    /**
     * Property change.
     * 
     * @param event the event
     * 
     * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
     */
    public void propertyChange(PropertyChangeEvent event) {
        startUrl = CommonHelper.getStartUrl();
        autoLoginEnabled = Activator.getDefault().getIsAutoLoginEnabled();
    }

    /**
     * Gets the display area.
     * 
     * @return the display area
     */
    public Composite getDisplayArea() {
        return displayArea;
    }
}
